/*
* SpecificHeatService.java
* Created on  202015/6/29 9:26
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/29       longshihui    初始版本
*
*/

package com.infitecs.eyas.enthalpy.service;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.enthalpy.dao.SpecificHeatDao;
import com.infitecs.eyas.enthalpy.model.Smoke;
import com.infitecs.eyas.enthalpy.model.SpecificHeat;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.eyas.util.Parameter;
import com.infitecs.eyas.util.ParametersUtil;
import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Service
public class SpecificHeatService {
    @Autowired
    private SpecificHeatDao specificHeatDao;
    @Autowired
    private ExpressionDao expressionDao;

    public void updateKx(Param param){
        specificHeatDao.updateKx(param);
    }

    public SpecificHeat getSpecificHeat(int caseId){
        return specificHeatDao.getSpecificHeat(caseId);
    }

    public void addSpecificHeat(int caseId){
        specificHeatDao.addSpecificHeat(caseId);
    }

    public Double calK5(Param param){
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SPECIFICHEAT_K5.toString());
        SpecificHeat specificHeat = specificHeatDao.getSpecificHeat(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("K1",specificHeat.getK1()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateKx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Double calK6(Param param){
        SpecificHeat specificHeat = specificHeatDao.getSpecificHeat(param.getCaseId());
        Formula formula;
        Set<Parameter> params = new HashSet<>();
        params.add(new Parameter("K1",specificHeat.getK1()));
        params.add(new Parameter("K4",specificHeat.getK4()));
        if (specificHeat.getK4() > 10) {
            formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SPECIFICHEAT_K6_1.toString());
        }
        else {
            formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SPECIFICHEAT_K6_2.toString());
        }
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateKx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Double calK7(Param param){
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SPECIFICHEAT_K7.toString());
        SpecificHeat specificHeat = specificHeatDao.getSpecificHeat(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("K1",specificHeat.getK1()));
            params.add(new Parameter("K2",specificHeat.getK2()));
            params.add(new Parameter("K3",specificHeat.getK3()));
            params.add(new Parameter("K6",specificHeat.getK6()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateKx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Double calK8(Param param){
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SPECIFICHEAT_K8.toString());
        SpecificHeat specificHeat = specificHeatDao.getSpecificHeat(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("K7",specificHeat.getK7()));
            params.add(new Parameter("K2",specificHeat.getK2()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateKx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }
}
